/**************************************************************************** 
* 
* Copyright (c) 2023  C*Core -   All Rights Reserved  
* 
* THIS SOFTWARE IS DISTRIBUTED "AS IS," AND ALL WARRANTIES ARE DISCLAIMED, 
* INCLUDING MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* 
* PROJECT     : CCFC2011BC           
* DESCRIPTION : stm demo code
* HISTORY     : Initial version.
* @file     stm_demo.c
* @version  1.1
* @date     2023 - 02 - 20
* @brief    Initial version.
* 
*****************************************************************************/
#include "stm_demo.h"
#include "console.h"
#include "intc_lld.h"
#include "siul_lld.h"
#include "led_hw.h"
#include "time_tick.h"
//#include "rtthread.h"
//#include "board.h"

/*STM channel 0 callback*/
static void STM_Callback0(void)
{
    /* call related callback */
    (void)PSPRINTF("STM Channel 0 expired!\n");
}

/*STM channel 1 callback*/
static void STM_Callback1(void)
{
    (void)PSPRINTF("STM Channel 1 expired!\r\n");
}

/*STM channel 2 callback*/
static void STM_Callback2(void)
{
    (void)PSPRINTF("STM Channel 2 expired!\r\n");
}

/*STM channel 3 callback*/
static void STM_Callback3(void)
{
    (void)PSPRINTF("STM Channel 3 expired!\r\n");
    STM_LLD_ResetCounter(&STMD);
}

static void STM_Callback_Test(void)
{
//    (void)PSPRINTF("STM Channel 0 expired!\r\n");
#if 0
    static uint8_t count = 0;
    if (count > 1)
    {
    
    /* enter interrupt */
    rt_interrupt_enter();

    rt_tick_increase();

    STM_LLD_ResetCounter(&STMD);
    /* leave interrupt */
    rt_interrupt_leave();
    }
    else
    {
    count++;
    STM_LLD_ResetCounter(&STMD);
    }
 //   INTC.EOIR.R = 0;
#endif
    STM_LLD_ResetCounter(&STMD);
}
void STM_LLD_ResetCounter_cbk(void)
{
	STM_LLD_ResetCounter(&STMD);
}
/**
 * @brief   STM configuration structure
 * @param  Channel enable or disable 
 * @param  Compare value for channel  
 * @param  Channel callback function 
*/
static STM_CH_Config stm_config[STM_CHANNELS] ={
    {TRUE, 0x100, STM_Callback0},
    {TRUE, 0x100, STM_Callback1},
    {TRUE, 0x100, STM_Callback2},
    {TRUE, 0x100, STM_Callback3},
};

/*
 *  time = cnt/(syc_clk/prescaler)
 */


/*******************************************************************************
 * @brief      STM_Test_Case1 STM single channel test
 * @param[in]  cnt      Compare value for channel
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static uint32_t STM_Test_Case1(uint32_t cnt) /* PRQA S 3219 */
{
    static STM_CH_Config stm_single_config[1] ={
    {TRUE, 0xFFFFFFFFu, tick_1ms},//STM_Callback_Test
    };
    /* stm driver initialization */
    STM_LLD_Init(&STMD, stm_single_config);
    /* Activates STM Driver */
    STM_LLD_Configure(&STMD);
    //(void)PSPRINTF("\n STM_Test_Case1 Start!\n");
    /* Enable channels and start global counter */
    STM_LLD_ModuleEnable(&STMD);
    /* Set module prescaler */
    STM_LLD_SetPrescaler(64);
    /* Set channel compare value */
    STM_LLD_SetCompareValue(0, cnt);
//    delayms(2000);
//    delayms(2000);
//    delayms(2000);
//    delayms(2000);
//    STM_LLD_DeInit(&STMD);
    //(void)PSPRINTF("\n STM_Test_Case1 End!\n");
    return 0;
}

/*******************************************************************************
 * @brief      STM_Test_Case2 STM all channel test
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static uint32_t STM_Test_Case2(void) /* PRQA S 3219 */
{
    /* STM driver initialization */
    STM_LLD_Init(&STMD, stm_config);
    /* Activates STM Driver */
    STM_LLD_Configure(&STMD);
    (void)PSPRINTF("\n STM_Test_Case2 Start!\n");
    /* Enable channels and start global counter */
    STM_LLD_ModuleEnable(&STMD);
    /* Set module prescaler */
    STM_LLD_SetPrescaler(64);
    /* Set channel compare value */
    STM_LLD_SetCompareValue(0, 1000);
    /* Set channel compare value */
    STM_LLD_SetCompareValue(1, 2000000);
    /* Set channel compare value */
    STM_LLD_SetCompareValue(2, 3000000);
    /* Set channel compare value */
    STM_LLD_SetCompareValue(3, 4000000);
//    delayms(2000);
//    delayms(2000);
//    delayms(2000);
//    delayms(2000);
//    STM_LLD_DeInit(&STMD);
    (void)PSPRINTF("\n STM_Test_Case2 End!\n");
    return 0;
}

/*******************************************************************************
 * @brief      STM_Test_Case3 STM delay test
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static uint32_t STM_Test_Case3(void) /* PRQA S 3219 */
{
    /* stm driver initialization */
    STM_LLD_Init(&STMD, stm_config);
#if 0
    uint32_t cnt1 = 0u;
    uint32_t cnt2 = 0u;
    (void)PSPRINTF("\n STM_Test_Case3 Start!\n");
    /* STM delay initialization */
    STM_LLD_DelayInit();
    for (cnt1 = 0u; cnt1 < 10u; cnt1++)
    {
        (void)LED2_IO_Set(1u);
        /* STM delay */
        STM_LLD_Delayms(1000u);
        (void)LED2_IO_Set(0u);
        /* STM delay */
        STM_LLD_Delayms(1000u);
    }
    for (cnt2 = 0u; cnt2 < 10u; cnt2++)
    {
        (void)LED2_IO_Set(1u);
        /* STM delay */
        STM_LLD_Delayus(1000u);
        (void)LED2_IO_Set(0u);
        /* STM delay */
        STM_LLD_Delayus(1000u);
    }
    (void)PSPRINTF("\n STM_Test_Case3 End!\n");
#endif
    return 0;
}

/*******************************************************************************
 * @brief      STM_Test_Case4 STM api test
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static uint32_t STM_Test_Case4(void) /* PRQA S 3219 */
{
    /* stm driver initialization */
    STM_LLD_Init(&STMD, stm_config);
    /* Activates STM Driver */
    STM_LLD_Configure(&STMD);
    /* Enable channels and start global counter */
    STM_LLD_ModuleEnable(&STMD);
    /* Set module prescaler */
    STM_LLD_SetPrescaler(32);
    /* Set channel state */
    STM_LLD_SetChannelState(0, 0);
    /* Set channel compare value */
    STM_LLD_SetCompareValue(0, 3000000);
    /* Set channel compare value */
    STM_LLD_SetCompareValue(1, 2000000);
    /* Set channel compare value */
    STM_LLD_SetCompareValue(2, 1000000);
    /* Set channel compare value */
    STM_LLD_SetCompareValue(3, 4000000);
    delayms(2000);
    delayms(2000);
    delayms(2000);
    delayms(2000);
    STM_LLD_DeInit(&STMD);
    (void)PSPRINTF("\n STM_Test_Case4 End!\n");
    return 0;
}

/*******************************************************************************
 * @brief      SWT_TEST_Demo
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void STM_TEST_Demo(void)
{
    /* Set the current priority */
    INTC_CPR_Init((uint8_t)CPR_PRI_0);
    //(void)PSPRINTF("\nTest End!\n");
    STM_LLD_SetDebugFreeze(FREEZE_MODE_OFF);

    /* Function: single channel open */
#if STM_CASE1
    (void)STM_Test_Case1(1000);
#endif
    /* Function: all channel open */
#if STM_CASE2
    (void)STM_Test_Case2();
#endif
    /* Function: stm delay test */
#if STM_CASE3
    (void)STM_Test_Case3();
#endif
    /* Function: stm api test */
#if STM_CASE4
    (void)STM_Test_Case4();
#endif
}



